<?php
namespace control;
/*
*规章制度类
*/


class regulationClass extends adIndexClass
{
    private const page = 'regulation'; # 权限
    private const title = '规章制度';
    private const keyword = '规章制度';
    public static function getAllChildTypeId($id): array
    {
        $typeIdArr = [];
        if (empty($id)) {
            return $typeIdArr;
        } else {
            $typeIdArr[] = $id;
            $material_type = query('document_catalog', "id = '$id'");
            $child = find(array('table' => 'document_catalog', 'where' => "pid = '$material_type[id]'"));
            foreach ($child['array'] as $array) {
                $result = self::getAllChildTypeId($array['id']);
                $typeIdArr = array_merge($typeIdArr, $result);
            }
        }
        return array_filter($typeIdArr);
    }
    public  function getDepartmentAll($data = []): string
    {
        $classify = query('document_catalog', "name = '" . self::keyword . "' order by time asc");
        $typeIdArr = self::getAllChildTypeId($classify['id']);
        //根据员工选择器的值和当前员工的部门，职位，员工ID求交集
        $idColloection = $this->staffCombination(true,self::$stid);
        // 点击事件
        $data['click'] = $data['click'] ?? "replaceDetail(this,'" . self::page . "')";
        $where = '1=1';
        if (!$this->power(self::page,'seeAll')){
            $where = "(concat(',',replace(selector,';',','),',') regexp concat(',',replace('{$idColloection}',',',',|,'),','))>0";
        }
        // 结果
        $result = find(['table' => 'document', 'where' => $where." and dcid in ('".implode("','",$typeIdArr)."') and state = '已发布' order by publicTime asc,time asc"]);
        $temp = "<ul pid='0'>";
        $defaultId = '';
        if ($result['array']) {
            foreach ($result['array'] as $k=>$value) {
                if ($k==0){
                    $defaultId = $value['id'];
                }
                $temp .= "<li class='arrow-no-li'  data-id='{$value['id']}' onclick=\"{$data['click']}\">{$value['title']}</li>";
            }
        }
        $temp .= '</ul>';
        $temp.=<<<eof
<script>
function search_left(e){
    containerLeftSearch(e,'{$this->get('cla')}')
}
 function cancelPage(id){
    let obj =  $("[data-id='"+id+"']");
    if (obj.length>0){
         replaceDetail(obj,'regulation');
    }
}
 $(function (){
     cancelPage('$defaultId')
 })
</script>
eof;

        return $temp;
    }


    /**
     * 左侧搜索框
     *
     * @return string
     */
    public function getContainerLeftJson(): string
    {
        $json['html'] = $this->getDepartmentAll([]);
        return json_encode($json, 320);
    }


    public function homeHtml(): string
    {
        //返回
        $search = '<i class="container-left-search-ico"></i>' . text(["name" => 'name', 'title' => "搜索文章", 'class' => 'container-left-search', 'keyup' => 'search_left']);
        $space = space;
        $all = $this->getDepartmentAll([]);
        return <<<EOT
        <div class="container-main">
            <div class="container-left left" id="container-left"  style="position: relative">
                {$search}
                {$space}
                {$all}
            </div>
            <div class="container-right left" id="container-detail">
            </div>
        </div>
<style>
.container-left{height: calc(100% - 90px);position: fixed !important;min-height: unset;overflow-y: auto;overflow-x: hidden;}
/*.document-body{height: 700px;overflow: auto}*/
.container-right{height: calc(100% - 90px);margin-left: 18%;width: 80%}
</style>
EOT;
    }

    /*列表页-查询条件*/
    public function home()
    {

        //返回
        return $this->homeFun($this->homeHtml());
    }




    public function detail()
    {
        $info = query('document', "id = '".$this->post('id')."' ");
        if (empty($info['id'])) {
            $json['html'] = "未找到此文章";
        } else {
            //按钮
            //生成表单
            $data['html'] = $this->tableShow([
                [
                    [
                        '<h3>' . $info['title'] . '</h3>',
                    ],
                    [
                        "发布人" => self::findStaff($info['publicStid']),
                        "发布时间" => self::null($info['publicTime']),
                    ],
                    [
                        self::htmlDecode($info['content'], false),
                    ],
                    [
                        '附件' => fileUploadClass::showFile($info['id'], '文档管理')
                    ]
                ]
            ]);
            //生成页面
            $json['html'] = '<div class="document-body">' . $data['html'] . '</div>';
        }
        //返回
        return json_encode($json, 320);
    }
}
